/**
 * COPYRIGHT© KMTC 2007
 * This software is the proprietary information of Korea Marine Transport Co.LTD
 * 
 * Revision History
 * Author			Date		Description
 * ------			----		-----------
 * KIMJUHEE@wees.co.kr		
 */
package kr.or.javacafe.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.apache.commons.logging.*;

import java.util.Locale;

/**
 * @author codeguru
 *
 */
public class SessionLocaleController implements Controller {
	
	public static final int KOREA_LOCALE = 1;
	public static final int ENGLISH_LOCALE = 2;
	public static final int JAPAN_LOCALE = 4;
	public static final int CHINESE_LOCALE = 3;
	
	protected final static Log logger = LogFactory.getLog(SessionLocaleController.class);

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		int localeMode = RequestUtils.getIntParameter(request, "locale", 1);
		
		Locale locale = null;
		if( localeMode == KOREA_LOCALE ){
			locale = Locale.KOREAN;
		}else if( localeMode == ENGLISH_LOCALE ){
			locale = Locale.ENGLISH;
		}else if( localeMode == JAPAN_LOCALE ){
			locale = Locale.JAPANESE;
		}else if( localeMode == CHINESE_LOCALE ){
			locale = Locale.CHINESE;
		}
		
		HttpSession session = request.getSession();
		session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);		
		logger.debug("locale ==>"+locale);
		
		return new ModelAndView(new RedirectView("/"));
	}

}
